package com.lz.util;


import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.ImageOptions;
import com.yinhai.activity.R;
import com.yinhai.activity.SMSService;
import com.yinhai.map.ServiceMap;
import com.yinhai.util.UuId;


public class Utils {
	public static final  int IsImgSize=100;
	
	/**
	 * 单位通讯录
	 * */
	public static void SaveDWTXL(Context activity, boolean isdwtxl){
		SystemParsUtils.getInstance(activity).setBoolean(SystemParsUtils.KEY_ISDWTXL, isdwtxl);
	}
	
	/**
	 * 单位通讯录
	 * */
	public static boolean getDWTXL(Context activity){
		return SystemParsUtils.getInstance(activity).getBoolean(SystemParsUtils.KEY_ISDWTXL);
	}
	
	
	/**
	 * 是同步单位，还是同步本地通讯录
	 * */
	public static void SaveIsDWTXL(Activity activity, boolean isdwtxl){
		SystemParsUtils.getInstance(activity).setBoolean(SystemParsUtils.KEY_ISDWTXLIS, isdwtxl);
	}
	
	/**
	 * 是同步单位，还是同步本地通讯录
	 * */
	public static boolean getIsDWTXL(Context activity){
		return SystemParsUtils.getInstance(activity).getBoolean(SystemParsUtils.KEY_ISDWTXLIS);
	}
	
	/**
	 * 保存是否自动回复
	 * */
	public static void SaveAutoIsZDHF(Activity activity, boolean isAuto){
		SystemParsUtils.getInstance(activity).setBoolean(SystemParsUtils.KEY_ISAUTOLOGIN, isAuto);
	}
	
	/**
	 * 获取是否自动回复
	 * */
	public static boolean getAutoIsZDHF(Context activity){
		return SystemParsUtils.getInstance(activity).getBooleanDefaultTrue(SystemParsUtils.KEY_ISAUTOLOGIN);
	}
	
	/**
	 * 开始日期
	 * */
	public static void SaveKSDATE(Activity activity, Long date){
		SystemParsUtils.getInstance(activity).setLong(SystemParsUtils.KEY_KSDATE, date);
	}
	
	/**
	 * 开始日期
	 * */
	public static Long getKSDATE(Context activity){
		return SystemParsUtils.getInstance(activity).getLong(SystemParsUtils.KEY_KSDATE);
	}
	
	
	/**
	 * 结束日期
	 * */
	public static void SaveJSDATE(Activity activity, Long date){
		SystemParsUtils.getInstance(activity).setLong(SystemParsUtils.KEY_JSDATE, date);
	}
	
	/**
	 * 结束日期
	 * */
	public static Long getJSDATE(Context activity){
		return SystemParsUtils.getInstance(activity).getLong(SystemParsUtils.KEY_JSDATE);
	}
	
	
	/**
	 * 发送日期
	 * */
	public static void SaveFSDATE(Context activity, Long date){
		SystemParsUtils.getInstance(activity).setLong(SystemParsUtils.KEY_FSDATE, date);
	}
	
	/**
	 * 发送日期
	 * */
	public static Long getFSDATE(Context activity){
		return SystemParsUtils.getInstance(activity).getLong(SystemParsUtils.KEY_FSDATE);
	}
	
	
	/**
	 * 短信内容
	 * */
	public static void SaveDXContent(Activity activity, String content){
		SystemParsUtils.getInstance(activity).setString(SystemParsUtils.KEY_DXCONTENT, content);
	}
	
	/**
	 * 短信内容
	 * */
	public static String getDXContent(Context activity){
		return SystemParsUtils.getInstance(activity).getString(SystemParsUtils.KEY_DXCONTENT);
	}
	
	
	/**
	 * 是否第一次登陆
	 * */
	public static void SaveIsLogin(Activity activity, int Islogin){
		SystemParsUtils.getInstance(activity).setInt(SystemParsUtils.KEY_ISLOGIN, Islogin);
	}
	
	/**
	 * 是否第一次登陆
	 * */
	public static int getIsLogin(Context activity){
		return SystemParsUtils.getInstance(activity).getInt(SystemParsUtils.KEY_ISLOGIN);
	}
	
	
	/**
	 * KEY_LOCAL_SERVICE
	 */
	public static void SaveService(Context activity, String service){
		SystemParsUtils.getInstance(activity).setString(SystemParsUtils.KEY_LOCAL_SERVICE, service);
	}
	
	/**
	 *  KEY_LOCAL_SERVICE
	 * */
	public static String GetService(Context activity){
		return SystemParsUtils.getInstance(activity).getString(SystemParsUtils.KEY_LOCAL_SERVICE);
	}
	
	/**
	 * 
	 * */
	public static void SaveADVersionNUM(Activity activity, int adversion){
		SystemParsUtils.getInstance(activity).setInt(SystemParsUtils.KEY_ADVERSION, adversion);
	}
	
	/**
	 * 
	 * */
	public static int getADVersionNUM(Context activity){
		return SystemParsUtils.getInstance(activity).getInt(SystemParsUtils.KEY_ADVERSION);
	}
	
	/**
	 * 启动 自动回复
	 * @param context
	 */
	public static void startMsgService(Context context){
		Intent intent = new Intent(context.getApplicationContext(), SMSService.class);
		ServiceMap.getMap().put(UuId.getUUID(), intent);
		context.startService(intent);
	}
	
	public static void stopMsgService(Context context){
		Intent intent = new Intent(context.getApplicationContext(), SMSService.class);
		context.stopService(intent);
	}
	public static void stopMsgService(Context context,Class claze){
//		 try {
//				XmppConnectionManager.getInstance()
//					.getConnection().getAccountManager().deleteAccount();
//		} catch (XMPPException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//		} 
		Intent intent = new Intent(context.getApplicationContext(), claze);
		context.stopService(intent);
	}

	
	
	/**
	 * 显示Toast
	 * */
	public static void showToast(Context activity, String content){
		View toastRoot = LayoutInflater.from(activity).inflate(R.layout.mytoast, null);
		Toast toast=new Toast(activity);
		toast.setView(toastRoot);
		toast.setGravity(Gravity.CENTER, 0, 0);
		TextView tv=(TextView)toastRoot.findViewById(R.id.tvContent);
		tv.setText(content);
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.show();
	}
	
	
	/**
	 * 弹出相册选项框
	 * */
	@SuppressWarnings("deprecation")
	public static AlertDialog showSetImageDialog(Activity thisRef, String title, String delText, OnClickListener selectL, OnClickListener takeL, OnClickListener delL){
		final AlertDialog dialog = new AlertDialog.Builder(thisRef).create();
		dialog.setCanceledOnTouchOutside(false);
	 	dialog.show();
		Window window = dialog.getWindow();
	 	window.setContentView(R.layout.dialog_setimage);
	 	WindowManager windowManager = thisRef.getWindowManager();
		Display display = windowManager.getDefaultDisplay();
		WindowManager.LayoutParams lp = window.getAttributes();
	//	int margin = thisRef.getResources().getDimensionPixelOffset(R.dimen.space_20);
		//lp.width = (int)(display.getWidth()) - (margin*2);
		lp.height= display.getHeight()*3 / 5 ;
		lp.gravity = Gravity.CENTER_HORIZONTAL;
		window.setAttributes(lp);
		
	 	((TextView)window.findViewById(R.id.tvTitle)).setText(title);
	 	Button btnSelect = (Button)window.findViewById(R.id.btnSelect);
	 	Button btnTake = (Button)window.findViewById(R.id.btnTake);
	 	Button btnDel = (Button)window.findViewById(R.id.btnDel);
	 	Button btnCancel = (Button)window.findViewById(R.id.btnCancel);
	 	
	 	btnSelect.setOnClickListener(selectL);
	 	btnTake.setOnClickListener(takeL);
	 	
 		if(!delText.trim().equals("") && delL!=null){
	 		btnDel.setText(delText);
	 		btnDel.setOnClickListener(delL);
	 	}else{
	 		btnDel.setVisibility(View.GONE);
	 	}
	 	btnCancel.setOnClickListener(new View.OnClickListener() {
        	public void onClick(View v) {
        		dialog.cancel();
        	}
        });
	 	return dialog;
	}
	
	/**
	 * 圆形图片
	 * @param bitmap
	 * @param pixels
	 * @return
	 */
	public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {   
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);   
        Canvas canvas = new Canvas(output);   
        final int color = 0xff424242;   
        final Paint paint = new Paint();   
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());   
        final RectF rectF = new RectF(rect);   
        final float roundPx = pixels;   
        paint.setAntiAlias(true);   
        canvas.drawARGB(0, 0, 0, 0);   
        paint.setColor(color);   
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);   
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));   
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }
	
	/**
	 * 获取加载网络图片选项
	 * */
	public static ImageOptions getImageOptions(Context thisRef, int defaultImage,int round){
		ImageOptions options = new ImageOptions();
		options.round = round;
		options.memCache = false;
		options.fileCache = false;
		options.targetWidth = 0;
		options.fallback = defaultImage;
		options.preset = BitmapFactory.decodeResource(thisRef.getResources(), defaultImage);
		options.animation = AQuery.FADE_IN_NETWORK;
		return options;
	}
	
	/**
     * 获取加载网络图片选项
     * */
  /*  public static ImageOptions getImageOptionsTrue(Context thisRef, int defaultImage,int round){
        ImageOptions options = new ImageOptions();
        options.round = round;
        options.memCache = false;
        options.fileCache = false;
        options.targetWidth = 0;
        options.fallback = defaultImage;
        options.preset = BitmapFactory.decodeResource(thisRef.getResources(), defaultImage);
        options.animation = AQuery.FADE_IN_NETWORK;
        return options;
    }*/
    
	/**
	 * 获取信息提示声音
	 * @param activity
	 * @return
	 */
	/*public static String getSound(Context activity) {
		String value = SystemParsUtils.getInstance(activity).getString(SystemParsUtils.SOUND).equals("")?"nosound":SystemParsUtils.getInstance(activity).getString(SystemParsUtils.SOUND);
		return value;
	}
	
	public static void setSound(Context activity,String sound) {
		SystemParsUtils.getInstance(activity).setString(SystemParsUtils.SOUND, sound);
	}*/
	
	/**
	 * 获取信息提示声音 , true 声音  ， false 震动
	 * */
	public static void setSound(Activity activity, boolean sound){
		SystemParsUtils.getInstance(activity).setBoolean(SystemParsUtils.SOUND, sound);
	}
	
	/**
	 * 获取信息提示声音
	 * */
	public static boolean getSound(Context activity){
		return SystemParsUtils.getInstance(activity).getBoolean(SystemParsUtils.SOUND);
	}
	
	/**
	 * 获取当前时间
	 */
	public String getDate() {
		Calendar c = Calendar.getInstance();
		String year = String.valueOf(c.get(Calendar.YEAR));
		int month = (c.get(Calendar.MONTH) + 1);
		int day = c.get(Calendar.DAY_OF_MONTH);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int mins = c.get(Calendar.MINUTE);
		int sec = c.get(Calendar.SECOND);

		StringBuffer sbBuffer = new StringBuffer();
		sbBuffer.append(year + "-" + (month<10?"0"+month:month) + "-" + (day<10?"0"+day:day) + " " + (hour<10?"0"+hour:hour) + ":"
				+ (mins<10?"0"+mins:mins) + ":" + (sec<10?"0"+sec:sec));
		return sbBuffer.toString();
	}
	
	
	
	
	public static AlertDialog showSetMsgDialog(Activity thisRef, OnClickListener selectqf, OnClickListener takedx){
        final AlertDialog dialog = new AlertDialog.Builder(thisRef).create();
//        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_set_msg);
        WindowManager windowManager = thisRef.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = window.getAttributes();
    //  int margin = thisRef.getResources().getDimensionPixelOffset(R.dimen.space_20);
        //lp.width = (int)(display.getWidth()) - (margin*2);
//        lp.height= display.getHeight()*3 / 5 ;
        lp.gravity = Gravity.CENTER_HORIZONTAL;
        window.setAttributes(lp);
        
        Button btnSelect = (Button)window.findViewById(R.id.btnSelect);
        Button btnTake = (Button)window.findViewById(R.id.btnTake);
        
        btnSelect.setOnClickListener(selectqf);
        btnTake.setOnClickListener(takedx);
        
        return dialog;
    }
}
